﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using W.Common.Enum;
using W.Purchase.API.Application.Command.ProcurementCommand;
using W.Purchase.Domain;
using W.Purchase.Repository.Impl;
using W.Purchase.Repository.Interface;

namespace W.Purchase.API.Application.CommandHandler.ProcurementCommandHandler
{
    public class ProcurementCreateCommandHandler : IRequestHandler<ProcurementCreateCommand, ApiResult<int>>
    {
        private readonly ILogger<ProcurementCreateCommandHandler> _logger;
        private readonly IProcurementRepository _procurementRepository;
        private readonly IBuyProductsRepository _buyProductsRepository;
        public ProcurementCreateCommandHandler(ILogger<ProcurementCreateCommandHandler> logger, IProcurementRepository procurementRepository, IBuyProductsRepository buyProductsRepository)
        {
            _logger = logger;
            _procurementRepository = procurementRepository;
            _buyProductsRepository = buyProductsRepository;
        }
        public async Task<ApiResult<int>> Handle(ProcurementCreateCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            _logger.LogInformation("添加采购成功");
            //System.Data.IsolationLevel.Serializable
            using (var tran = _procurementRepository.DB.Database.BeginTransaction(System.Data.IsolationLevel.Serializable))
            {
                try
                {
                    //采购表添加一条数据
                    var now = DateTime.Now;
                    Procurement procurement = new Procurement()
                    {
                        ProcurementNo = request.ProcurementNo,
                        ProcurementTypeName = request.ProcurementTypeName,
                        AssociateNumber = request.AssociateNumber,
                        VendorNo = request.VendorNo,
                        ClientName = request.ClientName,
                        ProcurementPerson = request.ProcurementPerson,
                        ProcurementPhone = request.ProcurementPhone,
                        Harvestay = request.Harvestay,
                        ProcurementRemark = request.ProcurementRemark,
                        SingleMaker = request.SingleMaker,
                        SingleTime = request.SingleTime,
                        //ProcurementState = ProcurementState.创建订单,
                        //IsRecorded = request.IsRecorded,
                        //IsReturn = request.IsReturn,
                        CreateTime = now,
                        office_id = request.office_id,
                        user_id = request.user_id,
                        IsDelete = false,
                    };
                    await _procurementRepository.AddAsync(procurement);
                    //给采购产品信息添加
                    foreach (var item in request.BuyProducts)
                    {
                        BuyProducts buyProducts = new BuyProducts()
                        {
                            ProcurementId = procurement.ProcurementId,
                            ProductNo = item.ProductNo,
                            ProductName = item.ProductName,
                            ProductSpec = item.ProductSpec,
                            UnitName = item.UnitName,
                            Price = item.Price,
                            BuyProductsNum = item.BuyProductsNum,
                            ProcurementState = ProcurementState.创建订单,
                            IsRecorded = item.IsRecorded,
                            IsReturn = item.IsReturn,
                            BuyProductsTotal = item.BuyProductsTotal,
                            office_id = item.office_id,
                            user_id = item.user_id,
                            CreateTime = now,
                            IsDelete = false,
                        };
                        await _buyProductsRepository.AddAsync(buyProducts);
                    }
                    var list = await _procurementRepository.DB.SaveChangesAsync();
                    tran.Commit();
                    result.Code = list;
                }
                catch (Exception el)
                {
                    result.Code = -1;
                    result.Message = el.Message;
                    _logger.LogError(el, el.Message);
                    tran.Rollback();
                }
            }
            return result;
        }
    }
}
